package com.canbot.u05.utils.image;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;

/**
 * Created by zmp on 2017/9/6.
 */

public class ImageDao {

        /**
         * @param absolutePath 图片绝对路径
         * @param isDeleteSdk  是否去除缩略图中Sdk路径
         * @return 图片缩略图路径
         * <p>
         * 用图库打开才有缩略图
         */
        public static String getThumbPath(Context context, String absolutePath, boolean isDeleteSdk) {
                String thumbPath = null;
                Cursor query = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                                  new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "='" + absolutePath + "'",
                                                                  null, null);
                if (query.moveToNext()) {
                        String a = query.getString(query.getColumnIndex(MediaStore.Images.Media._ID));
                        Log.d("Test", "a:" + a);
                        Cursor query2 = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                                                                           new String[]{
                                                                                   MediaStore.Images.Thumbnails.DATA
                                                                           }, MediaStore.Images.Thumbnails.IMAGE_ID + "='" + a + "'", null, null);
                        if (query2.moveToNext()) {
                                thumbPath = query2.getString(query2.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
                                Log.d("Test", "thumbPath:" + thumbPath);
                                if (!TextUtils.isEmpty(thumbPath) && isDeleteSdk) {
                                        thumbPath = thumbPath.replace(Environment.getExternalStorageDirectory().getAbsolutePath(), "");
                                }
                                Log.d("Test", "thumbPath:" + thumbPath);
                        }
                        query2.close();
                }
                query.close();
                return thumbPath;
        }

        /**
         * @return 图片缩略图路径
         * <p>
         * 用图库打开才有缩略图
         */
        public static ArrayList<String> getAllImagePath(Context context) {
                ArrayList<String> list = new ArrayList<>();
                Cursor query = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                                                  null, null, null, null);
                if (query == null) {
                        return list;
                }
                while (query.moveToNext()) {
                        String path = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
                        list.add(path);
                }
                query.close();
                return list;
        }
}
